package util

import (
	"os/exec"
	//"syscall"
	"context"
	"fmt"
	"os"
	"time"
)

type Deamon struct {
	Ctx    context.Context
	Cancel context.CancelFunc
	Cmd    *exec.Cmd
}

func NewDeamonController() *Deamon {
	return &Deamon{nil, nil, nil}
}

func (this *Deamon) CreateDeamon(process string, processargs string) {

	(*this).Ctx, (*this).Cancel = context.WithCancel(context.Background())
	(*this).Cmd = exec.CommandContext((*this).Ctx, process, processargs)
	//(*this).Cmd.SysProcAttr 	= &syscall.SysProcAttr{Setpgid: true, }
	(*this).Cmd.Stdout = os.Stdout
	(*this).Cmd.Start()

}
func (this *Deamon) ExitDeamon() {

	time.Sleep(10 * time.Second)
	fmt.Println("退出程序中...", (*this).Cmd.Process.Pid)
	(*this).Cancel()
	(*this).Cmd.Wait()
}
